﻿using System;
using System.Collections.Generic;
using System.Linq;

using RecruitmentAgency.Models.DataTypes;

namespace RecruitmentAgency.Models.DataProviders
{
    /// <summary>
    /// Provides database access for job provider data
    /// </summary>
    public class JobProviderDataProvider : BaseDataProvider, IJobProviderDataProvider
    {        
        /// <summary>
        /// Get a person Id number from a person username
        /// </summary>
        /// <param name="jobProviderId">The ID number of the JobProvider instance to get</param>
        /// <returns>A JobProvider instance</returns>
        public JobProvider GetJobProvider(int jobProviderId)
        {
            JobProvider company = new JobProvider();
            try
            {
                List<up_GetJobProviderResult> results = DataContext.up_GetJobProvider(jobProviderId).ToList();
                if (results.Count > 0)
                {
                    up_GetJobProviderResult result = results[0];
                    company.JobProviderId = result.JobProviderId;
                    company.AddressLine1 = result.AddressLine1;
                    company.AddressLine2 = result.AddressLine2;
                    company.Town = result.City;
                    company.PostCode = result.PostCode;
                    company.CompanyName = result.CompanyName;
                    company.Phone = result.Phone;
                    company.RegistrationNumber = result.RegistrationNumber;
                    company.Email = result.Email;
                    company.UserName = result.UserName;
                    company.Title = result.Title;
                    company.FirstName = result.FirstName;
                    company.LastName = result.LastName;
                    company.Sector = result.SectorName;
                    company.WebsiteUrl = result.WebsiteUrl;
                }
            }
            catch (Exception ex)
            {
                DataProvider.HandleException(ex); 
            }
            return company;
        }

        /// <summary>
        /// Gets a JobProvider instance from a person ID number
        /// </summary>
        /// <param name="personId">The ID number of the JobProvider instance to get</param>
        /// <returns>A JobProvider instance</</returns>
        public JobProvider GetJobProviderByPersonId(int personId)
        {
            JobProvider company = new JobProvider();
            try
            {
                List<up_GetJobProviderByPersonIdResult> results = DataContext.up_GetJobProviderByPersonId(personId).ToList();
                if (results.Count > 0)
                {
                    up_GetJobProviderByPersonIdResult result = results[0];
                    company.JobProviderId = result.JobProviderId;
                    company.AddressLine1 = result.AddressLine1;
                    company.AddressLine2 = result.AddressLine2;
                    company.Town = result.City;
                    company.PostCode = result.PostCode;
                    company.CompanyName = result.CompanyName;
                    company.Phone = result.Phone;
                    company.RegistrationNumber = result.RegistrationNumber;
                    company.Email = result.Email;
                    company.UserName = result.UserName;
                    company.Title = result.Title;
                    company.FirstName = result.FirstName;
                    company.LastName = result.LastName;
                    company.Sector = result.SectorName;
                    company.WebsiteUrl = result.WebsiteUrl;
                }
            }
            catch (Exception ex)
            {
                DataProvider.HandleException(ex);
            }
            return company;
        }

        /// <summary>
        /// Updates a job provider from a JobProvider instance
        /// </summary>
        /// <param name="jobProvider">The JobProvider instance to update</param>
        public void UpdateJobProvider(JobProvider jobProvider)
        {
            try
            {
                DataContext.up_UpdateJobProvider(jobProvider.JobProviderId, jobProvider.CompanyName, jobProvider.RegistrationNumber, jobProvider.AddressLine1, jobProvider.AddressLine2, jobProvider.Town, jobProvider.PostCode, jobProvider.Title, jobProvider.FirstName, jobProvider.LastName, jobProvider.Phone, jobProvider.Email, jobProvider.WebsiteUrl.ToLower().Replace("www.", string.Empty).Replace("http://", string.Empty), jobProvider.Sector);
            }
            catch (Exception ex)
            {
                DataProvider.HandleException(ex);
            }
        }

        /// <summary>
        /// Deletes the job provider associated with the jobProviderId parameter
        /// </summary>
        /// <param name="jobProviderId">The ID number of the JobProvider instance to delete</param>
        public void DeleteJobProvider(int jobProviderId)
        {
            try
            {
                DataContext.up_DeleteJobProvider(jobProviderId);
            }
            catch (Exception ex)
            {
                DataProvider.HandleException(ex);
            }
        }

        /// <summary>
        /// Gets the job provider results and paging data
        /// </summary>
        /// <param name="pageSize">The number of rows of results to a page</param>
        /// <param name="pageNumber">The number of the page of results to return</param>
        /// <param name="firstName">The first name of the job provider to search for</param>
        /// <param name="lastName">The last name of the job provider to search for</param>
        /// <param name="companyName">The company name of the job provider to search for</param>
        /// <param name="registrationNumber">The company registration number of the job provider to search for</param>
        /// <param name="city">The city of the job provider to search for</param>
        /// <returns>A JobProviderSearchData instance that holds the job provider results and paging data</returns>
        public JobProviderSearchData SearchJobProviders(int pageSize, int pageNumber, string firstName, string lastName, string companyName, string registrationNumber, string city)
        {
            JobProviderSearchData searchData = new JobProviderSearchData
            {
                PageSize = pageSize,
                PageNumber = pageNumber
            };
            try
            {
                foreach (up_SearchJobProvidersResult result in DataContext.up_SearchJobProviders(pageSize, pageNumber, firstName, lastName, companyName, registrationNumber, city))
                {
                    searchData.Results.Add(new JobProvider
                    {
                        JobProviderId = (int) result.JobProviderId,
                        CompanyName = result.CompanyName,
                        FirstName = result.FirstName,
                        LastName = result.LastName,
                        Email = result.EmailWork,
                        Town = result.City,
                        WebsiteUrl = result.WebsiteUrl
                    });
                    searchData.TotalResultCount = (int)result.TotalResultCount;
                }
            }
            catch (Exception ex)
            {
                DataProvider.HandleException(ex);
            }
            return searchData;
        }
    }
}